package leetcode;

import java.util.ArrayList;

public class ZigZagConversion {

	public static void main(String[] args) {
		String string = "PAY";
		ZigZagConversion object = new ZigZagConversion();
		System.out.println(object.convert(string, 2));
	}
	
	public String convert(String s, int numRows) {
		if(s == null || s.length() <= 0 || numRows <= 1){
			return s;
		}
		int length = s.length();
		ArrayList<ArrayList<Character>> lists = new ArrayList<ArrayList<Character>>();
		int index = 0;
		for(int i = 0; i < numRows; i++){
			lists.add(new ArrayList<Character>());
		}
		boolean down = true;
		for(int i = 0; i < length; i++){
			if(down){
				lists.get(index++).add(s.charAt(i));
				System.out.println("down: " + index);
				if(index == numRows - 1){
					down = false;
				}
			}else{
				lists.get(index--).add(s.charAt(i));
				System.out.println("up: " + index);
				if(index == 0){
					down = true;
				}
			}
		}
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < numRows; i++){
			for (Character character : lists.get(i)) {
				sb.append(character);
			}
		}
		return sb.toString();
    }
}
